package com.example.filecabinet.controller;

import com.aliyuncs.exceptions.ClientException;
import com.example.filecabinet.biz.BizController;
import com.example.filecabinet.dao.Lattice_infoMapper;
import com.example.filecabinet.dto.req.BizUserQueryReq;
import com.example.filecabinet.dto.req.StoreQueryReq;

import com.example.filecabinet.entity.Lattice_info;
import com.example.filecabinet.entity.Lattice_infoExample;
import com.example.filecabinet.service.StaffMemberService;
import com.example.filecabinet.util.SmsUtils;
import com.thfdcsoft.framework.common.http.dto.BaseHttpRspn;
import com.thfdcsoft.framework.common.json.jackson.JacksonFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class StaffMemberController {
    @Autowired
    private StaffMemberService staffMemberService;
    @Autowired
    Lattice_infoMapper Lattice_infoMapper;

    private static Logger logger = LoggerFactory.getLogger(StaffMemberController.class);

    /**
     * 工作人员存件
     * 客户端请求业务层查询接口信息 = {"application/json;charset=UTF-8"},
     */
    @RequestMapping(value = "/staffMembersave", method = RequestMethod.POST)
    @ResponseBody
    public String staffMemberSave(HttpServletRequest request, HttpServletResponse response, @RequestBody String ywh) {
        logger.info("【工作人员存】接收的参数为：" + ywh);
        StoreQueryReq storeQueryReq = JacksonFactory.readJson(ywh, StoreQueryReq.class);
        BaseHttpRspn result = new BaseHttpRspn();

        //去数据库查询是否已存过
        Lattice_infoExample lattice_infoExample = new Lattice_infoExample();
        Lattice_infoExample.Criteria criteria = lattice_infoExample.createCriteria();
        criteria.andBusinessNumberEqualTo(storeQueryReq.getBusinessNumber());
        criteria.andDeviceNumberEqualTo(storeQueryReq.getDeviceNumber());
        criteria.andLatticeLdleStatusEqualTo("01");  //占用
        criteria.andLatticeSwithStatusNotEqualTo("02");    //异常格子
        criteria.andLatticeOperatorEqualTo("00");     //操作者工作人员

        List<Lattice_info> lattice_infos = Lattice_infoMapper.selectByExample(lattice_infoExample);
        if (null != lattice_infos && lattice_infos.size() > 0) {      //已存件
            System.out.println("已存件");
            result.setResult(true);
            result.setRespMsg("已存件");
            result.setRespJson(JacksonFactory.writeJson(lattice_infos.get(0)));
            return JacksonFactory.writeJson(result);
        } else {
            System.out.println("未存件");//第一次存件
            String s = staffMemberService.staffMemberUrl(storeQueryReq.getBusinessNumber());
            logger.info("工作人员查询接口信息：" + s);
            return s;
        }
    }

    /**
     * 普通人查询待存业务
     * 客户端请求业务层查询接口信息
     * 姓名 身份证
     */
    @RequestMapping(value = "/generalUserSave", method = RequestMethod.POST)
    @ResponseBody
    public String staffMemberSave2(HttpServletRequest request, HttpServletResponse response, @RequestBody String ywh) {
        logger.info(ywh);
        BizUserQueryReq bizUserQueryReq = JacksonFactory.readJson(ywh, BizUserQueryReq.class);
        String recordUserName = bizUserQueryReq.getRecordUserName();
        logger.info("传入参数名字：" + recordUserName);
        String s = BizController.generalUserUrl(ywh);
        System.out.println("普通人查询待存业务--" + s);
        return s;
    }

    /**
     * 发送短信
     */
    @RequestMapping(value = "staffMemberTel", method = RequestMethod.POST)
    @ResponseBody
    public String staffMemberTel(HttpServletRequest request, HttpServletResponse response, @RequestBody String tel) {
        logger.info("【工作人员存】发送短信的手机号为：" + tel);
        BaseHttpRspn result = new BaseHttpRspn(false);
        Boolean sms = null;
        try {
            sms = SmsUtils.Sms(tel);
            if (sms) {
                result.setResult(true);
                result.setRespMsg("短信发送成功");
            } else {
                result.setResult(false);
                result.setRespMsg("短信发送失败");
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return JacksonFactory.writeJson(result);
    }
}
